package GameObjects.Instances;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Shape;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class Instances 
{
	private ArrayList<Instance> instances;
	//images say 1 till 10 is an animation that is played over and over
	//after 10 is when it is dead
	private int startOfAnimation;
	private int endOfAnimation;
	
	private int currentInstance = 0;
	
	public Instances(ArrayList<Instance> instances, int endOfAnimation)
	{
		this.instances = instances;
		this.startOfAnimation = 0;
		this.endOfAnimation = endOfAnimation;
	}
	
	public void deathAnimation()
	{
		this.startOfAnimation = this.endOfAnimation;
		this.currentInstance = this.endOfAnimation;
		this.endOfAnimation = this.instances.size();
	}
	
	public void replaceInstances(ArrayList<Instance> instances)
	{
		this.instances = instances;
		this.startOfAnimation = this.endOfAnimation;
		this.currentInstance = this.endOfAnimation;
		this.endOfAnimation = this.instances.size();
	}
	
	public Image image()
	{
		return instances.get(currentInstance).IMAGE;
	}
	
	public void tick()
	{
		currentInstance++;
		if(currentInstance == endOfAnimation)
			currentInstance = this.startOfAnimation;
	}
	
	public void render(Graphics2D g2d, double x, double y, double theta)
	{
		int width = instances.get(currentInstance).WIDTH;
		int height = instances.get(currentInstance).HEIGHT;
		BufferedImage image = instances.get(currentInstance).IMAGE;
		if(image != null)
		{
			g2d.rotate(theta, x+width/2, y+height/2);
			g2d.drawImage(image, (int)x, (int)y, width, height, null);
			g2d.rotate(-theta, x+width/2, y+height/2);
		}
	}
	
	public double width()
	{
		return instances.get(currentInstance).WIDTH; 
	}
	
	public double height()
	{
		return instances.get(currentInstance).HEIGHT; 
	}
	
	public Shape transformInstance(double x, double y, double theta)
	{
		return instances.get(currentInstance).transformBounds(x, y, theta);
	}
}
